package javapop.framework.parser;

import java.util.regex.MatchResult;

import javapop.framework.ParseContext;
import javapop.framework.ParseInput;
import javapop.framework.ParseResult;
import javapop.framework.Parser;

public class RegexpString extends Parser<String> {
	private Parser<String> child;
	
	public RegexpString(String regexp) {
		child = new XForm<MatchResult,String>(new Regexp(regexp)) {
			@Override
			public String transform(ParseContext<?> ctx, MatchResult content) {
				return content.group();
			}
		};
	}
	
	@Override
	public String getDescription() {
		return child.getDescription();
	}
	
	@Override
	public boolean isMemoized() {
		return true;
	}
	
	@Override
	public int hashCode() {
		return child.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj==null) return false;
		if(obj==this) return true;
		if(obj.getClass()!=this.getClass())
			return false;
		return ((RegexpString) obj).child.equals(child);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ParseResult<String> eval(ParseContext ctx, ParseInput input) {
		return child.parse(ctx, input);
	}
	
}
